package projet.item;

import projet.Tools;

import projet.CustomException;
import projet.dataaccess.DataAccess;
import projet.dataaccess.PathErrorException;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

/**
 * The Photo class represents a Son object. Information about the Photo is
 * stored and can be retrieved. We assume that we only deal with movie Son at
 * this stage.
 * 
 * @author BRUN Sylvain
 * @version Janvier 27, 2010
 */
public class Son extends Item {

	private String artist;
	private Integer time;

	/**
	 * @param id
	 * @throws CustomException
	 */
	public Son(int id) throws CustomException {
		super();
		this.id = id;
		this.AskEdit();
	}

	/**
	 * @param id
	 * @param theTitle
	 * @param comment
	 * @param theArtist
	 * @param path
	 */
	public Son(int id, String theTitle, String comment, String theArtist,
			Integer time, String path) {
		super(id, theTitle, comment, path);
		this.artist = theArtist;
		this.path = path;
		this.time = time;
	}

	/**
	 * @param dataAccess
	 * @throws PathErrorException
	 */
	public Son(DataAccess dataAccess) throws PathErrorException {
		super(dataAccess);
		this.artist = dataAccess.getElementString("artist");
		this.time = dataAccess.getElementInt("time");
		this.path = dataAccess.getElementString("path");

	}

	/*
	 * (non-Javadoc)
	 * @see projet.item.Item#AskEdit()
	 */
	@Override
	public void AskEdit() throws CustomException {
		super.AskEdit();

		// Artist
		System.out.print("- Artist: ");
		this.artist = Tools.readLineEdit(this.artist);

		// Time
		System.out.print("- Time: ");
		this.time = Integer.parseInt(Tools.readLineEdit(this.time));

		// Time
		System.out.print("- Path: ");
		this.path = Tools.readLineEdit(this.path);

	}

	/*
	 * (non-Javadoc)
	 * @see projet.item.Item#play()
	 */
	@Override
	public void play() {
		if (Desktop.isDesktopSupported()) {
			if (Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
				try {
					Desktop.getDesktop().open(new File(path));
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	/**
	 * @return The artist for this Son.
	 */
	public String getArtist() {
		return this.artist;
	}

	/**
	 * @return The time for this Son.
	 */
	public int getTime() {
		return this.time;
	}

	/*
	 * (non-Javadoc)
	 * @see projet.item.Item#toMediaAccess()
	 */
	public DataAccess toMediaAccess() throws PathErrorException {
		DataAccess dataAccess = super.toMediaAccess();
		dataAccess.addElement("artist", this.artist);
		dataAccess.addElement("path", this.path);
		dataAccess.addElement("time", this.time.toString());

		return dataAccess;
	}

	/**
	 * Méthode affichage pour un show
	 */
	public void affichage() {
		super.affichage();
		Tools.print(2, "Artist      : " + this.artist);
		Tools.print(2, "Duration    : " + this.time);
	}

	/*
	 * (non-Javadoc)
	 * @see Item#toString()
	 */
	@Override
	public String toString() {
		return super.toString() + "Artist:\"" + this.artist + "\" Time:\""
				+ this.time;
	}

}
